;; file/resolve.scm - (c) rohan drape, 2003-2005

;; The path list searched by 'resolve'.  This cannot be called
;; resolve-path since that name is used by an mzscheme primitive.
;; This is not intended for direct manipulation.

(define-global resolver-path (list))

;; Add `path' to the front of the resolve path.

(define (resolver-path-add! path)
  (resolver-path (cons path (resolver-path))))

;; Return a fully qualified path name for `partial-file-name' by
;; searching the list of files at `file-path', an ordinary scheme
;; list.  If no path exists '#f' is returned. The `partial-file-name'
;; is allowed to name an existing file.

(define (resolveP path name)
  (cond ((file-exists? name) name)
	((null? path) #f)
	(else
	 (let ((nameR (path->string (build-path (car path) name))))
	   (if (file-exists? nameR) nameR (resolveP (cdr path) name))))))

;; Locate the file `name' along the search path 'resolver-path'.  If no
;; file is located return '#f'.

(define (resolve name) (resolveP (cons (rsc-directory) (resolver-path)) name))

;; A variant that raises an error if no file exists.

(define (resolve* name)
  (let ((r (resolve name)))
    (or r (error "resolve*: failed" name (resolver-path)))))
